/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1/commercial
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may use this file in compliance with the License.
 * Alternatively, if you can't copy with the License, namely if you do not
 * want to provide source code of your modifications to this file, you may
 * negotiate a commercial licence with the initial developer.
 * You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is COID.
 *
 * The Initial Developer of the Original Code is
 * PosAm.
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 * Robert Strycek
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "server.h"

#ifdef _DEBUG

namespace coid {

//static
void CoidNode::create_list( MX_REGISTER_ITERATOR & it, const dynarray<thread> * threads, dynarray<const comm_mutex *> & mxlist,
                  time_t time )
{
	DASSERT( it.size() );
	const comm_mutex * p;


	uint now = SINGLETON(MsecTimer).time();


	if( threads == NULL ) {		// list all
		mxlist.reserve( it.size(), true );
		while( 1 ) {
			p = it.next();
			if( ! p ) break;

			uint time0 = (uint) p->get_locktime();
			if( !time0 ) continue;		// not locked
			uint delta = now - time0;
			if( delta < (uint)time ) continue;	// locked shorter amount of time than specified

			*mxlist.add(1) = p;
		}
		return;
	}

	while(1)
    {
		p = it.next();
		if( ! p ) break;

		uint time0 = (uint)p->get_locktime();
		if( !time0 ) continue;		// not locked
		uint delta = now - time0;
		if( delta < (uint)time ) continue;	// locked shorter amount of time than specified

		thread_t t = p->get_owner();

		//const thread* u = *threads;
		for( uint i=0; i<(*threads).size(); i++ ) {
            if( (*threads)[i] == t ) {
				*mxlist.add(1) = p;
				break;
			}
		}
	}
}



opcd CoidNode::create_list( charstr& out, time_t time, bool all ) const
{
	dynarray<const comm_mutex *> mxlist;
	if( all ) {
		MUTEX_ITERATOR(mxit);
		create_list( mxit, NULL, mxlist, time );
		if( mxlist.size() )
			format_list( mxlist, out, mxlist.size() );
		return 0;
	}

    if( _thr_own.size() == 0 && _thr_using.size() == 0 )
		return 0;

	MUTEX_ITERATOR(mxit);
    if( _thr_own.size() )
		create_list( mxit, &_thr_own, mxlist, time );
	uints owned = mxlist.size();
    if( _thr_using.size() )
		create_list( mxit, &_thr_using, mxlist, time );

    if( mxlist.size() )
		format_list( mxlist, out, owned );

	return 0;
}


//static
void CoidNode::format_list( const dynarray<const comm_mutex *>& mxlist, charstr& out, uints owned )
{
	if( !mxlist.size() ) return;

	uint now = SINGLETON(MsecTimer).time();

	out << "<html><table align=\"center\" border=\"1\" cellspacing=\"1\" cellpadding=\"1\"><tbody>";

	out << "<tr>";
	out << "<td colspan=\"3\">mutex</td>";
	out << "<td colspan=\"1\">owner</td>";
	out << "<td colspan=\"3\">thread</td>";
	out << "</tr>";


	thread_info ti;

	uints i=0;
	const comm_mutex * const * m = mxlist.ptr();
	for( i=0; i<mxlist.size(); i++, m++ ) {
		out << "<tr>";

		if( i == owned && owned ) out << "<td colspan=\"7\">owned:</td></tr><tr>";

		const comm_mutex * mx = *m;
		out << "<td>" << (uints) (const void *) mx << "</td>";
		out << "<td>" << mx->get_name() << "</td>";
		uint delta = now - (uint)mx->get_locktime();
		out << "<td>" << delta << " ms" << "</td>";

		uint t = (uint) mx->get_owner();
		if( 0 == SRVROOTA._thrmgr.get_thread_info( t, ti) ) {
			uint obj = mx->get_objid();
			if( obj != UMAX32 ) 
				out << "<td><a href=\"select:" << obj << "\">" << obj << "</a></td>";
			else out << "<td></td>";

			out << "<td><a href=\"select:" << ti._tempid << "\">" << ti._tempid << "</a></td>";
			out << "<td>" << ti._name << "</td>";
			out << "<td>" << t << "</td>";
		}

		out << "</tr>";		//out << "\n";
	}

	out << "</table>";
}


}	// namespace coid


#endif		// _DEBUG





















